<template>
  <div class="department-container">
    <div class="app-container">
      <!-- 头部 -->
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-card>

      <!-- 分层 -->
      <el-tree :data="list" :default-expand-all="true">
        <template #default="{ data }">
          <el-row
            type="flex"
            justify="space-between"
            align="middle"
            style="height: 40px; width: 100%">
            <el-col :span="20">
              <span>{{ data.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{ data.manager }}</el-col>
                <el-col>
                  <!-- 下拉菜单 element -->
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <!-- 下拉菜单 -->
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                      <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-tree>

      <!-- 对话框 -->
      <el-dialog
        :title="isEdit ? '编辑' : '添加'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"> 
        <!-- 在弹层中放入子组件 -->
        <deptDialog  
        :id='curId' 
        v-if="showDialog"
        :is-edit="isEdit"
        :origin-list="originList"
        @close="hclose"
        @success="hSuccess"/>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments,delDepartment } from "@/api/departments";
import { translation } from "@/utils";
import deptDialog from "./deptDialog.vue";

export default {
  components: {
    deptDialog,
  },
  data() {
    return {
      showDialog: false, // 控制对话框的显示隐藏
      list: [], // 存储部门列表数据
      curId:'', // 保存当前部门id
      isEdit:false, // 是否处于编辑状态
      originList:[]  // 保存校验的数据
    };
  },
  created() {
    this.loadDepartments();
  },
  methods: {
    // 点击取消按钮,关闭弹层
    hclose(){
      this.showDialog = false
    },
    // 删除部门
    hDel(id){
      this.$confirm('确定要删除吗?, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then( async () => {
           await delDepartment(id)
          this.loadDepartments()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 编辑弹框
    hEdit(id){
     // 1.弹层出来
       this.showDialog = true
     // 2. 表单中要显示详情数据
       this.curId = id
     // 3. 传递一个标识给子组件,辨识现在是编辑状态
       this.isEdit = true
    },
    // 点击确定添加,进行重新渲染数据
    hSuccess(){
     // 重新渲染部门列表
     this.loadDepartments()
     // 关闭弹层
     this.showDialog = false
    },
    // 用户点击了添加子部门,保存id
    hAdd(id) {
      // 显示弹层
      this.showDialog = true;
      // 更新当前操作的id
      this.curId = id;
      // 传递标识给子组件
      this.isEdit = false
    },
    // 获取部门列表
    async loadDepartments() {
      try {
        const res = await getDepartments();
        console.log(res);
        this.originList = res.data.depts.map(item =>{
         return {id:item.id,pid:item.pid,name:item.name,code:item.code}
        })
        // shift会取出数组第一个元素,同时改变原数组
        res.data.depts.shift();
        this.list = translation(res.data.depts);
      } catch (error) {
        console.log(err);
      }
    },
  },
};
</script>